package com.kuzufang.modules.search;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kuzufang.R;
import com.kuzufang.base.BaseAdapter;
import com.kuzufang.bean.SubwayBean;
import com.kuzufang.utils.ConfigLoader;
import com.kuzufang.utils.DisplayUtils;
import com.kuzufang.utils.JsonUtil;
import com.kuzufang.view.CircularTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songyuanjin on 2017/9/21.
 */

public class SubWayLineAdapter extends BaseAdapter<SubwayBean, SubWayLineAdapter.SubWayHolder> {
    List<SubwayBean> subwayBeanList = new ArrayList<>();

    public SubWayLineAdapter(Context ctx) {
        super(ctx);

        String subwayConfig = ConfigLoader.config(ctx);
        subwayBeanList = JsonUtil.convert2Array(subwayConfig, SubwayBean.class);

        clear();
        addAll(subwayBeanList);
        notifyDataSetChanged();
    }

    private int selectedPosition = 0;

    @Override
    protected void bindViewHolderData(SubWayHolder viewHolder, SubwayBean data, int position) {
        viewHolder.bindData(data, position);
    }

    @Override
    public SubWayHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.item_subway_line_search, parent, false);
        return new SubWayHolder(view);
    }

    class SubWayHolder extends RecyclerView.ViewHolder {
        private CircularTextView circularTextView;

        public SubWayHolder(View itemView) {
            super(itemView);
            circularTextView = (CircularTextView) itemView.findViewById(R.id.subway_line);
        }

        public void bindData(final SubwayBean data, final int position) {
            if (data.isChecked()) {
                AnimatorSet animatorSet = new AnimatorSet();
                ValueAnimator textSizeAnimator = ValueAnimator.ofInt(circularTextView.getTextSize(), 18).setDuration(200);
                textSizeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int sp = (int) animation.getAnimatedValue();
                        circularTextView.setShowTextSize(sp);
                        circularTextView.invalidate();
                    }
                });
                ValueAnimator valueAnimator = ValueAnimator.ofInt(34, 44).setDuration(200);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int dp = DisplayUtils.dp2px((int) animation.getAnimatedValue());
                        ViewGroup.LayoutParams layoutParams = circularTextView.getLayoutParams();
                        layoutParams.width = dp;
                        layoutParams.height = dp;
                        circularTextView.setLayoutParams(layoutParams);
                    }
                });
                animatorSet.playTogether(textSizeAnimator, valueAnimator);
                animatorSet.start();
            } else {
                int dp = DisplayUtils.dp2px(34);
                ViewGroup.LayoutParams layoutParams = circularTextView.getLayoutParams();
                layoutParams.width = dp;
                layoutParams.height = dp;
                circularTextView.setLayoutParams(layoutParams);
                circularTextView.setShowTextSize(14);
                circularTextView.invalidate();
            }
            circularTextView.setColorAndText(Color.parseColor(data.getLineColor()), data.getSubwayLine());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.isChecked()) {
                        return;
                    }
                    for (int i = 0; i < subwayBeanList.size(); i++) {
                        if (i != position) {
                            subwayBeanList.get(i).setChecked(false);
                        } else {
                            subwayBeanList.get(i).setChecked(true);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
        }

    }


    public void setSelectedPosition(int i) {
        selectedPosition = i;
        notifyDataSetChanged();
    }
}
